import React, { PureComponent } from "react";
import { connect } from "react-redux";
import ProjectList from "components/ProjectList/Home";
import server from "app/server";
import {callApi} from "app/utils";

let flag = true
export default class extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      itemData: [],
      opt: {
        page: 1,
        size: 0
      }
    };
    this.handleScrollMore = this.handleScrollMore.bind(this);
  }
  //销毁时解绑事件
  componentWillUnmount() {
    window.removeEventListener("scroll", this.handleScrollMore,true);
    //重写组件的setState方法，直接返回空
    this.setState = (state,callback)=>{
      return;
    };  
  }
  handleScrollMore() {
     console.log('flag:'+flag)
    if($(window).scrollTop()!=0){
      if (
        $(window).scrollTop() + window.innerHeight >=
        document.documentElement.scrollHeight-50&&flag == true
      ) {
        flag = false
        // console.log('flag:'+flag)
        let page = this.state.opt.page;
        page++;
        this.setState({
          opt: {
            page: page
          }
        });
        let tempList = this.state.itemData;
        // server.directList(this.state.opt.page, 10).then(data => {
        //   if (data.msg == "查询结果为空！") {
        //     flag = true
        //     // console.log('flag:'+flag)
        //     return;
        //   }
        //   this.setState({
        //     itemData: tempList.concat(data.data.loanList)
        //   });
        //   flag = true
        //   // console.log('flag:'+flag)
        // }).catch(()=>{
        //   flag=true
        // });
        callApi('/loan/list',{page:this.state.opt.page, size:10},"POST").then((data)=>{
          if (data.msg == "查询结果为空！") {
                flag = true
                return;
              }
              this.setState({
                itemData: tempList.concat(data.data.loanList)
              });
              flag = true
        }).catch(()=>{
            flag=true
          });
      }
    }
    
  }

  componentDidMount() {
    window.addEventListener("scroll", this.handleScrollMore,true);

    // server.directList(this.state.opt.page, 10).then(data => {
    //   this.setState({
    //     itemData: data.data.loanList
    //   });
    // });
    callApi('/loan/list',{page:this.state.opt.page, size:10},"POST").then((data)=>{
      this.setState({
        itemData: data.data.loanList
      });
    })
  }

 

  render() {
    const { config, focus, navlink, link, shop ,directOrAssign} = this.props;
    const {  itemData } = this.state;
    return (
      <div>
        <ProjectList 
          itemData={itemData}
          directOrAssign={directOrAssign} />
      </div>
    );
  }
}
